/*
Author: Oliver A. Nina
Copyrights: This code is open source. You can use the code for personal or comercial purposes. 
Disclaimer: Code AS IS. There is no warranty on using this code. Use this code at your own discretion.
If you use this code please cite the paper at : Interactive Enhancement on Handwritten Text trough Multi-resolution Gaussian, ICFHR, Bari, Italy 2012
*/

#include "stdafx.h"
#include "TextEnhancer.h"
#include <math.h>


TextEnhancer::TextEnhancer(IplImage *image,int stroke_width,int kernel_size_increment, int enhancement_value,int cursor_width){
	this->image = cvCloneImage(image);
	this->stroke_width = stroke_width;
	this->kernel_size_increment = kernel_size_increment;
	this->rimage = cvCloneImage(image);
	this->dimage = cvCloneImage(image);
	this->roi = cvCloneImage(image);
	this->sroi = cvCloneImage(image);
	this->bilateral_image = cvCloneImage(image);
	this->map = cvCreateImage(cvGetSize(image),IPL_DEPTH_32F,1);
	this->cursor_width = cursor_width;
	this->enhancement_value = enhancement_value;
	cvZero(this->map);
}

void TextEnhancer::apply_around(int x, int y){
	for(int i=y-this->stroke_width; i<y+this->stroke_width;i++){
		for(int j=x-this->stroke_width; j<x+this->stroke_width;j++){
			this->apply(i,j);
			
		}
	}
}

void TextEnhancer::apply(int i, int j) {// ~i=y j=x

	if(i>0 && j>0 && i<image->height && j< image->width){

		CvScalar blueColor = CV_RGB(0,0,255);
		//cvSet2D(this->dimage,i,j,blueColor);
		CV_IMAGE_ELEM(this->dimage,uchar,i,j) = 255;
		uchar pixel_map = CV_IMAGE_ELEM(this->map,uchar,i,j);
		uchar new_value = pixel_map + this->enhancement_value;
		CvScalar new_pixel;
		new_pixel.val[0]= new_value;
		//cvSet2D(this->map,i,j,new_pixel);
		CV_IMAGE_ELEM(this->map,uchar,i,j) = new_value;

	}
}

bool TextEnhancer::is_inside_boundaries(int m,int n){
	if(m>-1 && n>-1 && m<image->height && n <image->width)
		return true;
	else 
		return false;
}

int TextEnhancer::max(int num1, int num2){
	if(num1>num2)
		return num1;
	else
		return num2;
}

int TextEnhancer::get_value(IplImage * image,int i,int j)
{
	//CvScalar pixel = cvGet2D(image,i,j);
	//int pixelValue = pixel.val[0];
	int pixelValue = CV_IMAGE_ELEM(image,uchar,i,j);
	return pixelValue;
}
